package com.sugar.Stack;

/*
* 使用数组实现栈
* */
public class ArrayStack {
    private int maxSize;

    private int[] stackArray;

    private int top;

    public ArrayStack(int size){
        maxSize = size;
        stackArray = new int[maxSize];
        top = -1;
    }

    public void push(int element){
        if(top == maxSize - 1){
            throw new RuntimeException("Stack is full");
        }
        stackArray[++top] = element;
    }

    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("Stack is empty");
        }
        int e = stackArray[top--];
        return e;
    }

    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("Stack is empty");
        }
        return stackArray[top];
    }

    public boolean isEmpty(){
        return top==-1;
    }

    public int size(){
        return top + 1;
    }

}
